/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelDeVideo.java
 *
 * Created on 03-ene-2012, 8:57:02
 */

package GUI;

import java.awt.Component;
import java.awt.Dimension;
import java.io.IOException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JProgressBar;

/**
 *
 * @author Administrador
 */
public class PanelDeVideo extends javax.swing.JPanel {

    private Player mediaplayer;
    private Component video;

    /** Creates new form PanelDeVideo */
    public PanelDeVideo(URL url, Dimension d) {
        try {
            Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
            mediaplayer = Manager.createRealizedPlayer(url);
            video = mediaplayer.getVisualComponent();
            video.setSize(d);
            add(video);

            initComponents();
        } catch (IOException ex) {
            Logger.getLogger(PanelDeVideo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoPlayerException ex) {
            Logger.getLogger(PanelDeVideo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CannotRealizeException ex) {
            Logger.getLogger(PanelDeVideo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public PanelDeVideo(URL url) {
        try {
            mediaplayer = Manager.createRealizedPlayer(url);
        } catch (IOException ex) {
            Logger.getLogger(PanelDeVideo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoPlayerException ex) {
            Logger.getLogger(PanelDeVideo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CannotRealizeException ex) {
            Logger.getLogger(PanelDeVideo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    

    public float getVolumen(){
        return mediaplayer.getGainControl().getLevel();
    }

    public void setVolumen(float volumen){
        mediaplayer.getGainControl().setLevel(volumen);
    }

    public void mute(){
        if(mediaplayer.getGainControl().getMute()){
            mediaplayer.getGainControl().setMute(false);
        }else{
            mediaplayer.getGainControl().setMute(true);
        }
    }

    public void stop(URL url){
        mediaplayer.stop();
        if((!(url.toString().endsWith("mp3"))))
            video.hide();
    }

    public void play(URL url){
        mediaplayer.start();
        if((!(url.toString().endsWith("mp3"))))
            video.show();
    }

    private TimerTask task ;
    private int frame = 0;
    private Timer tiempo;
    private int speed = 50;

    public void startAnimation(final JProgressBar bar ,final URL url){
        tiempo = new Timer();
        task = new TimerTask() {

            @Override
            public void run() {
                frame = (int) Math.round((mediaplayer.getMediaTime().getSeconds()*100)/(mediaplayer.getDuration().getSeconds()));
                if(mediaplayer.getMediaTime().getSeconds() == mediaplayer.getDuration().getSeconds()){
                    frame = 100;
                    stop(url);
                    stopAnimation(bar);
                }else{
                    bar.setValue(frame);
                }

            }
        };

        tiempo.schedule(task,0 , speed);
    }

    public void stopAnimation(final JProgressBar bar){
        tiempo.cancel();
        task.cancel();
        bar.setValue(frame);
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
